import 'package:dio/dio.dart';

import '../constants/sp_key.dart';
import '../services/config.dart';
import 'interceptor/token_interceptor.dart';
import 'sp_utils.dart';

class DioUtils {
  static Dio _dio = Dio();

  static DioUtils? _instance;

  static DioUtils get instance => _getInstance();

  static DioUtils _getInstance() {
    _instance ??= DioUtils._internal();
    return _instance!;
  }

  DioUtils._internal() {
    BaseOptions options = BaseOptions(
        // baseUrl: HttpConfig.baseURL,
        connectTimeout: const Duration(milliseconds: HttpConfig.timeout),
        responseType: ResponseType.json,
        contentType: "application/json; charset=utf-8");
    _dio = Dio(options);

    Dio tokenDio = Dio(options);
    _dio.interceptors.add(TokenInterceptor(dio: _dio, tokenDio: tokenDio));
  }

  Future<T> requestBaseUrl<T>(String url,
      {String method = HttpConfig.get,
      Map<String, dynamic>? params,
      Interceptor? inter}) async {
    String token = await SPUtils.get(keyToken) ?? "";
    int languageId = await SPUtils.get(keyLanguage) ?? 1;
    String projId = await SPUtils.get(keyProjId) ?? "";
    url = "${HttpConfig.baseURL}$url";
    print("$url+${params.toString()}");

    print("$languageId");

    // 1.创建单独配置
    final options = Options(method: method, headers: {
      "Authorization": "Bearer $token",
      "Accept-language": languageId == 1 ? "zh-TW,zh" : "en-US,en",
      "projId": projId
    });
    // 2.发送网络请求
    try {
      Response response =
          await _dio.request(url, data: params, options: options);
      print(response.data);
      return response.data;
    } on DioException catch (e) {
      return Future.error(e);
    }
  }

  Future<T> requestVersion<T>(String url,
      {String method = HttpConfig.get,
      Map<String, dynamic>? params,
      Interceptor? inter}) async {
    url = "${HttpConfig.baseURL}$url";
    print("$url+${params.toString()}");

    // 1.创建单独配置
    final options =
        Options(method: method, headers: {"token": HttpConfig.versionToken});
    // 2.发送网络请求
    try {
      Response response =
          await _dio.request(url, data: params, options: options);
      print(response.data);
      return response.data;
    } on DioException catch (e) {
      return Future.error(e);
    }
  }

  Future<T> download<T>(String url, dynamic savePath,
      {String method = HttpConfig.get,
      Map<String, dynamic>? params,
      Function(int, int)? onReceiveProgress,
      Interceptor? inter}) async {
    url = "${HttpConfig.baseURL}$url";
    print("$url+${params.toString()}");

    // 1.创建单独配置
    final options =
        Options(method: method, headers: {"token": HttpConfig.versionToken});
    // 2.发送网络请求
    try {
      Response response = await _dio.download(url, savePath,
          data: params, options: options, onReceiveProgress: onReceiveProgress);
      print(response.data);
      return response.data;
    } on DioException catch (e) {
      return Future.error(e);
    }
  }
}
